// 云函数入口文件
const cloud = require('wx-server-sdk')
const axios = require('axios')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })

const db = cloud.database()
const _ = db.command

// 云函数入口函数
exports.main = async event => {
  const { type, _ids } = event
  const collection = `${type}-cast`

  const {
    data: [cData],
  } = await db
    .collection(collection)
    .where({ _id: _.in(_ids) })
    .get()
  if (!cData) {
    throw new Error('cast not found')
  }
  const {
    data: [sData],
  } = await db.collection('screen').where({ _id: cData.screenId }).get()

  await db
    .collection(collection)
    .where({ _id: _.in(_ids) })
    .remove()

  await axios.get('http://124.70.50.10:33333/sendMsg', {
    params: { sn: sData.sn, msg: type === 'image' ? 'cast' : 'vidoe' },
  })
}
